{"componentChunkName":"component---src-templates-post-jsx","path":"/[BOJ]5014/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"b3394e53-5e9a-595e-8da4-bb3debd220c0","excerpt":"문제 강호는 코딩 교육을 하는 스타트업 스타트링크에 지원했다. 오늘은 강호의 면접날이다. 하지만, 늦잠을 잔 강호는 스타트링크가 있는 건물에 늦게 도착하고 말았다. 스타트링크는 총 F층으로 이루어진 고층 건물에 사무실이 있고, 스타트링크가 있는 곳의 위치는 G층이다. 강호가 지금 있는 곳은 S층이고, 이제 엘리베이터를 타고 G층으로 이동하려고 한다. 보통…","html":"<h2>문제</h2>\n<p>강호는 코딩 교육을 하는 스타트업 스타트링크에 지원했다. 오늘은 강호의 면접날이다. 하지만, 늦잠을 잔 강호는 스타트링크가 있는 건물에 늦게 도착하고 말았다.</p>\n<p>스타트링크는 총 F층으로 이루어진 고층 건물에 사무실이 있고, 스타트링크가 있는 곳의 위치는 G층이다. 강호가 지금 있는 곳은 S층이고, 이제 엘리베이터를 타고 G층으로 이동하려고 한다.</p>\n<p>보통 엘리베이터에는 어떤 층으로 이동할 수 있는 버튼이 있지만, 강호가 탄 엘리베이터는 버튼이 2개밖에 없다. U버튼은 위로 U층을 가는 버튼, D버튼은 아래로 D층을 가는 버튼이다. (만약, U층 위, 또는 D층 아래에 해당하는 층이 없을 때는, 엘리베이터는 움직이지 않는다)</p>\n<p>강호가 G층에 도착하려면, 버튼을 적어도 몇 번 눌러야 하는지 구하는 프로그램을 작성하시오. 만약, 엘리베이터를 이용해서 G층에 갈 수 없다면, \"use the stairs\"를 출력한다.</p>\n<h3>입력</h3>\n<ul>\n<li>첫째 줄에 F, S, G, U, D가 주어진다. (1 ≤ S, G ≤ F ≤ 1000000, 0 ≤ U, D ≤ 1000000) 건물은 1층부터 시작하고, 가장 높은 층은 F층이다.</li>\n</ul>\n<h3>출력</h3>\n<ul>\n<li>첫째 줄에 강호가 S층에서 G층으로 가기 위해 눌러야 하는 버튼의 수의 최솟값을 출력한다. 만약, 엘리베이터로 이동할 수 없을 때는 \"use the stairs\"를 출력한다.</li>\n</ul>\n<h3>📍 <strong>Logic</strong></h3>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Pair {\n    int floor;\n    int cnt;\n\n    public Pair(int floor, int cnt) {\n        this.floor = floor;\n        this.cnt = cnt;\n    }\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>층 정보와 버튼 누른 횟수를 저장하기 위해 <code class=\"language-text\">Pair</code> <strong>class</strong>를 생성했다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">Queue&lt;Pair&gt; q = new LinkedList&lt;&gt;();\nq.add(new Pair(S, 0));\nvisited[S] = true;\n\nwhile(!q.isEmpty()) {\n    Pair now = q.poll();\n    int now_floor = now.floor;\n    int now_cnt = now.cnt;\n\n    if (now_floor == G) {\n        System.out.println(now_cnt);\n        return;\n    }\n\n    for (int go : upDown) {\n        int nxFloor = now_floor + go;\n        if (nxFloor &lt;= 0 || nxFloor &gt; F) continue;\n        if (visited[nxFloor]) continue;\n\n        q.add(new Pair(nxFloor, now_cnt + 1));\n        visited[nxFloor] = true;\n    }\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>현재 층에서 목표 층까지 가기 위해 <strong>BFS</strong>로 이동 가능한 층과 버튼 누른 횟수를 <code class=\"language-text\">Queue</code> 에 저장한다.</li>\n</ul>\n<h3>📄 <strong>CODE</strong></h3>\n<details>\n  <summary>코드 보기/접기💫</summary>\n    <div markdown=\"1\" data-language=\"java\">\n<deckgo-highlight-code  terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">    import java.io.BufferedReader;\n    import java.io.IOException;\n    import java.io.InputStreamReader;\n    import java.util.LinkedList;\n    import java.util.Queue;\n\n    class Pair {\n        int floor;\n        int cnt;\n\n        public Pair(int floor, int cnt) {\n            this.floor = floor;\n            this.cnt = cnt;\n        }\n    }\n\n    public class Main {\n        public static void main(String[] args) throws IOException {\n            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n            String[] line = br.readLine().split(&quot; &quot;);\n            int F = Integer.parseInt(line[0]), S = Integer.parseInt(line[1]), G = Integer.parseInt(line[2]);\n            int[] upDown = {Integer.parseInt(line[3]), -Integer.parseInt(line[4])};\n            boolean[] visited = new boolean[F + 1];\n\n            Queue&lt;Pair&gt; q = new LinkedList&lt;&gt;();\n            q.add(new Pair(S, 0));\n            visited[S] = true;\n\n            while(!q.isEmpty()) {\n                Pair now = q.poll();\n                int now_floor = now.floor;\n                int now_cnt = now.cnt;\n\n                if (now_floor == G) {\n                    System.out.println(now_cnt);\n                    return;\n                }\n\n                for (int go : upDown) {\n                    int nxFloor = now_floor + go;\n                    if (nxFloor &lt;= 0 || nxFloor &gt; F) continue;\n                    if (visited[nxFloor]) continue;\n\n                    q.add(new Pair(nxFloor, now_cnt + 1));\n                    visited[nxFloor] = true;\n                }\n            }\n\n            System.out.println(&quot;use the stairs&quot;);\n\n            br.close();\n        }\n    }</code>\n        </deckgo-highlight-code>\n  </div>\n</details>\n<h3>✏️ <strong>Review</strong></h3>\n<ul>\n<li>BFS 문제라는 것을 알고 풀어서 아이디어는 금방 잡았던 문제이다. 알고리즘 분류를 안 보고 아이디어를 생각해내는 연습이 필요할 것 같다.</li>\n<li>저번과 동일하게 방문 여부를 저장하는 부분에서의 <strong>메모리 초과</strong>를 피할 수 없었다. 좀 더 자세하게 생각해보는 연습이 필요할 것 같다.</li>\n</ul>\n<h3>📕 출처</h3>\n<p><a href=\"https://www.acmicpc.net/problem/5014\">https://www.acmicpc.net/problem/5014</a></p>","frontmatter":{"title":"👩‍💻 5014. 스타트 링크","date":"February 21, 2022","update":"February 21, 2022","tags":["그래프이론","그래프탐색","BFS"],"series":"👩‍💻 BOJ"},"fields":{"slug":"/[BOJ]5014/","readingTime":{"minutes":4.355}}},"seriesList":{"edges":[{"node":{"id":"27733333-3aad-5771-a1a1-27b4de4b4a01","fields":{"slug":"/[BOJ]2225/"},"frontmatter":{"title":"👩‍💻 2225. 합분해"}}},{"node":{"id":"548ba188-2afe-5575-b79f-107c6c65b07d","fields":{"slug":"/[BOJ]1012/"},"frontmatter":{"title":"👩‍💻 1012. 유기농 배추"}}},{"node":{"id":"0c712680-130f-5fa5-8c3b-3632dd5d1af0","fields":{"slug":"/[BOJ]1260/"},"frontmatter":{"title":"👩‍💻 1260. DFS와 BFS"}}},{"node":{"id":"0e07a592-06ce-5e01-b5e1-95398330372e","fields":{"slug":"/[BOJ]1707/"},"frontmatter":{"title":"👩‍💻 1707. 이분그래프"}}},{"node":{"id":"df12bfec-ed5c-5fc4-8442-0a4b3f9759a3","fields":{"slug":"/[BOJ]2668/"},"frontmatter":{"title":"👩‍💻 2668. 숫자 고르기"}}},{"node":{"id":"01bbb6dc-812c-5223-87c5-53bf04c0d0a6","fields":{"slug":"/[BOJ]9466/"},"frontmatter":{"title":"👩‍💻 9466. 텀 프로젝트"}}},{"node":{"id":"abc306b3-c3a6-552a-8979-e82d506b3921","fields":{"slug":"/[BOJ]2146/"},"frontmatter":{"title":"👩‍💻 2146. 다리 만들기"}}},{"node":{"id":"b3394e53-5e9a-595e-8da4-bb3debd220c0","fields":{"slug":"/[BOJ]5014/"},"frontmatter":{"title":"👩‍💻 5014. 스타트 링크"}}},{"node":{"id":"be297703-e778-5727-8019-6954bc95da3a","fields":{"slug":"/[BOJ]11559/"},"frontmatter":{"title":"👩‍💻 11559. Puyo Puyo"}}},{"node":{"id":"ddf11545-d468-5269-94b1-5daa2720e886","fields":{"slug":"/[BOJ]17471/"},"frontmatter":{"title":"👩‍💻 17471. 게리맨더링"}}},{"node":{"id":"8d77114d-fc6e-5de2-a3ca-c3b250425a56","fields":{"slug":"/[BOJ]2437/"},"frontmatter":{"title":"👩‍💻 2437. 저울"}}},{"node":{"id":"9a8942ce-552f-51ae-b304-0d0fab8f2eba","fields":{"slug":"/[BOJ]14891/"},"frontmatter":{"title":"👩‍💻 14891. 톱니바퀴"}}},{"node":{"id":"f1c04e8a-df0a-5294-949f-5f3a483cbcb0","fields":{"slug":"/[BOJ]14503/"},"frontmatter":{"title":"👩‍💻 14503. 로봇 청소기"}}},{"node":{"id":"883b6ecf-7528-5c16-92fa-edf12a64e80e","fields":{"slug":"/[BOJ]3190/"},"frontmatter":{"title":"👩‍💻 3190. 뱀"}}},{"node":{"id":"099b2576-7d05-5872-a9f0-f1ee6efd3a9a","fields":{"slug":"/[BOJ]14502/"},"frontmatter":{"title":"👩‍💻 14502. 연구소"}}},{"node":{"id":"50eef059-4e55-5f14-a9cf-fb01b6506f02","fields":{"slug":"/[BOJ]17779/"},"frontmatter":{"title":"👩‍💻 17779. 게리맨더링 2"}}},{"node":{"id":"aa67d90b-4801-574c-b225-56c23737a5e2","fields":{"slug":"/[BOJ]2579/"},"frontmatter":{"title":"👩‍💻 2579. 계단 오르기"}}},{"node":{"id":"9db6b43b-963a-5ebd-b72b-69936cf37858","fields":{"slug":"/[BOJ]14499/"},"frontmatter":{"title":"👩‍💻 14499. 주사위 굴리기"}}},{"node":{"id":"6a79cddd-dc1b-5e34-8a84-af313c37de0e","fields":{"slug":"/[BOJ]15685/"},"frontmatter":{"title":"👩‍💻 15685. 드래곤 커브"}}},{"node":{"id":"5e001d34-d985-545b-83ec-7f1c7caea286","fields":{"slug":"/[BOJ]17144/"},"frontmatter":{"title":"👩‍💻 17144. 미세먼지 안녕!"}}},{"node":{"id":"1d132410-9377-5e55-85b9-4664ff64de73","fields":{"slug":"/[BOJ]9935/"},"frontmatter":{"title":"👩‍💻 9935. 문자열 폭발"}}},{"node":{"id":"e13ff8d6-9b02-56c0-83e2-1d3144f70e1d","fields":{"slug":"/[BOJ]16472/"},"frontmatter":{"title":"👩‍💻 16472. 고냥이"}}},{"node":{"id":"0f782160-aaa9-5c54-80c0-72a15172dd16","fields":{"slug":"/[BOJ]1083/"},"frontmatter":{"title":"👩‍💻 1083. 소트"}}},{"node":{"id":"fef58615-9e18-5f50-8d09-3d1e2839342f","fields":{"slug":"/[BOJ]20922/"},"frontmatter":{"title":"👩‍💻 20922. 겹치는 건 싫어"}}},{"node":{"id":"7ca89240-c65f-5cb4-aeef-c5f1605ebd00","fields":{"slug":"/[BOJ]16172/"},"frontmatter":{"title":"👩‍💻 16172. 나는 친구가 적다(Large)"}}}]},"previous":{"fields":{"slug":"/[BackEnd]REST/"},"frontmatter":{"title":"💻 REST"}},"next":{"fields":{"slug":"/[NextStep]11/"},"frontmatter":{"title":"📖 11장 의존관계 주입(DI)을 통한 테스트하기 쉬운 코드 만들기"}}},"pageContext":{"id":"b3394e53-5e9a-595e-8da4-bb3debd220c0","series":"👩‍💻 BOJ","previousPostId":"72a19297-193d-5bb7-895a-44f328517242","nextPostId":"168191bb-b6de-5d4d-9d33-b9b8183f3857"}},"staticQueryHashes":[]}